package JDBC;

import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.sql.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * 测试CLOB 文本大对象的使用
 */
public class demo8 {

    public static void main(String[] args) {
        Connection conn= null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Reader r = null;
        try {
            //加载类驱动
            Class.forName("com.mysql.jdbc.Driver");
            conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/zz?characterEncoding=UTF-8","root","123456");

//            ps = conn.prepareStatement("INSERT INTO user (userName,myInfo) VALUES (?,?)");
//            ps.setString(1,"张哲");
//            ps.setClob(2,new FileReader(new File("src/IO/dest.txt")));//将文本文件直接输入到数据库中
//            ps.executeUpdate();

            ps = conn.prepareStatement("SELECT * FROM user WHERE id=?");
            ps.setObject(1,"40024");
            rs =  ps.executeQuery();
            while (rs.next()){
                Clob c = rs.getClob("myInfo");//始终操作的是流
                r = c.getCharacterStream();
                int temp = 0;
                while ((temp=r.read())!=-1){
                    System.out.print((char) temp);
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(r!=null){
                    r.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if(rs!=null){
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(ps!=null){
                    ps.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(conn!=null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
